var fileArray;
var EID;
$(document).ready(function() {	
	EID = $("#eventId").text();
	// 事件补充窗体的定义
	$("#addMackupWindow").dialog({
		//position: 'top' ,
		autoOpen : false,
		minWidth : 520,
		modal : true,
		title : '事件补充',
		close : function() {
			return false;
			//window.location.href="/ag/wzp/event_findById.action?event.EId="+EID;
		}
	});
	//确定补充
	$("#okAdd").click(function(){
		var eid = $("#eventId").text();
		var content =$("#mackContent").val();
		if(content==""){
			alert("补充内容不能为空！")
			return;
		}
		var annexAddr = findAnnex();
		var annexName = findAnnexName()
		//alert("ok"+annexAddr);
		//alert(annexName);
		$.ajax({
			type: "post",
			data:{'mackup.tbEvent.EId':eid,'mackup.emContext':content,'annexAddr':annexAddr,'annexName':annexName},
			url: "wzp/eventMackup_save.action",
			success:function(data){
				if(data=="true"){
					$("#addMackupWindow").dialog('close');
					alert("事件补充成功！");
					//alert("mmmmm");
					window.location.href="/ag/wzp/event_findById.action?event.EId="+eid;
					/*
					$.ajax({
						type: "post",
						data:{'EId':eid},
						url: "wzp/eventMackup_findByEId.action",
						success:function(data){
							$("#mackups").empty();
							var strHTML = "";
							if(data.mackups.length>0){
								$.each(data.mackups,function(i,n){
									var j = i + 1;
									strHTML += "<tr><td bgcolor='#dceffd'>";
				          			strHTML += "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#84b1d3' >"; 
				          			strHTML += "<tr><td width='104'><span style='float: left; padding-top:20;width:104;text-align:right'>事件补充"+j+"：</span></td>";
				          			strHTML += "<td><table>";
				          			strHTML += "<tr height='26' ><td style='padding-bottom:5px;border-bottom:1px solid #84b1d3;width:2000px;'>补充人："+n.tbUser.UName+"   补充时间："+n.emTime.replace(/T/," ");
				          			strHTML += "</td></tr>";
				          			strHTML += "<tr ><td >补充内容：<div style='padding-left:40px;padding-top:5px;border-bottom:1px solid #84b1d3;'>"+n.emContext+"</div></td></tr>";
				          			strHTML += "<tr><td>附件：";
				          			if(n.tbMackupAnnexes.length>0){
				          				$.each(n.tbMackupAnnexes,function(k,m){
											strHTML += "<div style='padding-left:40px;padding-top:5px;'>" +
													"<a href=\"/ag/file/download.action?fileRealName="+m.maName+"&fileDiskURL="+m.maAddress+"&moduleID=103\">"+m.maName+"</a></div>";
										});
				          			}else{
				          				strHTML += "<div style='padding-left:40px;'>没有附件</div>";
				          			}
				          			
				          			strHTML += "</td></tr>"; 
				          			strHTML += "</table></td>";
				        			strHTML += "</table></td></tr>";
									
								});
							}else{
								strHTML += "<tr class='trtab'>";
								strHTML += "<td class='tdtab' colspan='3'>";
								strHTML += "<span style='padding-left:20'>没有补充内容<span></td></tr>";
							}
							$("#mackups").append(strHTML);
							$("#mackups").show();
						}
					});
					*/
				}else{
					alert("事件补充失败！");
					//window.location.href="/ag/wzp/event_findById.action?event.EId="+eid;
				}
			}
		});
		//
	});
	//取消补充
	$("#cancelAdd").click(function(){
		$("#addMackupWindow").dialog('close');
		//window.location.href="/ag/wzp/event_findById.action?event.EId="+EID;
	});
	$("#addAnnexWindow").dialog({
		//position: 'top' ,
		autoOpen : false,
		minWidth : 450,
		modal : true,
		title : '添加附件',
		close: function() {
			var myFileArr=getFileArray();
			//alert(myFileArr);
			$.each(myFileArr,function(i,n){
				//alert("id:"+n.crtName);
				//alert("realname:"+n.originName);
				var htmlStr="";
				htmlStr+="<div><span class='fn'>"
				htmlStr+=n.originName;
				htmlStr+="</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
				htmlStr+="<span class='crtName' style='display:none;'>";
				htmlStr+=n.crtName;
				htmlStr+="</span><span class='dell'>删除</span></div>";
				$('#annexs').show();
				$('#annexsArea').show().append(htmlStr);
			});
			$('.dell').unbind("click");
			$('.dell').click(function () {
				$(this).parent().remove();
			});
			//alert(myFileArr.length);
			//getPaList();
	 	}
	});
	$("#addAnnex").click(function(){
		fileArray = new Array();
		$("#fileQueue").empty();
		$("#addAnnexWindow").dialog('open');
	});
	$("#complete").click(function(){
		$("#addAnnexWindow").dialog('close');
		realName = $("#RealNameID").text();
		objectName = $("#ObjectNameID").text();
		$("#annexs").show();
		$("#annexsArea").show().append("<span>"+realName+"</span>");
	});
	$("#uploadify").uploadify({
	　'uploader': 'lzm/js/uploadify.swf',  
	　'script':'annexUpload!upload.action?EID='+EID,
	　//'script': 'servlet/Upload?name=yangxiang',  
	　'cancelImg': 'lzm/images/cancel.png',                  
	　'queueID' : 'fileQueue', //和存放队列的DIV的id一致  
	　//'fileDataName': 'fileupload', //必须，和以下input的name属性一致                   
	　'auto'  : false, //是否自动开始  
	　'multi': true, //是否支持多文件上传  
	  'buttonText': 'BROWSE', //按钮上的文字  
	　'simUploadLimit' : 1, //一次同步上传的文件数目  
	　'sizeLimit': 19871202, //设置单个文件大小限制，单位为byte  
	　'queueSizeLimit' : 10, //队列中同时存在的文件个数限制  
	　//'fileDesc': '支持格式:.doc', //如果配置了以下的'fileExt'属性，那么这个属性是必须的  
	　//'fileExt': '*.doc;', //允许的格式
	  'buttonImg': 'lzm/images/select_file.gif',//允许的格式
	　onComplete: function (event, queueID, fileObj, response, data) {  
	　	//alert("文件:" + fileObj.name + "上传成功");
		//alert(response);
		//alert(data.name);
		$('#fileQueue').append('<p>文件'+fileObj.name+'上传成功！</p>');
		var oneFileSucc=new fileClass();
		oneFileSucc.originName=fileObj.name;
		oneFileSucc.crtName=response;
		//alert("vv"+response);
		//alert("mm"+fileObj.name);
		fileArray.push(oneFileSucc);
		//$("#RealNameID").text(fileObj.name);
		//$("#ObjectNameID").text(response);
	　},  
	　onError: function(event, queueID, fileObj) {  
	　	alert("文件:" + fileObj.name + "上传失败");  
	　},  
	　onCancel: function(event, queueID, fileObj){  
	　	alert("取消了" + fileObj.name);  
	　} 
　});
});
	function getFileArray(){
		return fileArray;
	}
  	function fileClass(){
	    this.originName = "";
	    this.crtName = "";
	}
  	 
  	function findAnnex(){
		var aa=1;
		var fileStr="";
		$('#annexsArea .crtName').each(function(i,n){
			if(aa==1){
				aa=2;
			}else{
				fileStr+=",";
			}
			//alert("vvvv:"+$(this).text());
			fileStr+=$(this).text();
		});
		//alert(fileStr);
		return fileStr;
  	}
  	function findAnnexName(){
		var aa=1;
		var fileStr="";
		$('#annexsArea .fn').each(function(i,n){
			if(aa==1){
				aa=2;
			}else{
				fileStr+=",";
			}
			fileStr+=$(this).text();
		});
		//alert(fileStr);
		return fileStr;
  	}